package com.ws.shop.action.admin;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.ws.shop.bean.Page;
import com.ws.shop.entity.Accessory;
import com.ws.shop.entity.Category;
import com.ws.shop.entity.Goods;
import com.ws.shop.service.CategoryService;
import com.ws.shop.service.GoodsService;
import com.ws.shop.util.RequestUtil;
import com.ws.shop.util.StringUtil;

@Controller
@RequestMapping(value="/admin/goods/*")
public class GoodsAction extends BaseAdminAction{
	
	private static Logger log = Logger.getLogger(GoodsAction.class);
	@Autowired
	private GoodsService goodsService = null;
	@Autowired
	private CategoryService categoryService = null;
	
	@RequestMapping(value="index.jhtml",method=RequestMethod.GET)
	public String index(HttpServletRequest request,Model model){
		String goodsname = RequestUtil.getStringParameter(request, "goodsname", null);
		int pageno = RequestUtil.getIntParameter(request, Page.PAGE_NO, 1);
		Page page = goodsService.findGoodsByPage(pageno,20,goodsname);
		model.addAttribute("goodsname", goodsname);
		model.addAttribute(Page.PAGE, page);
		return "goods_index";
	}
	
	@RequestMapping(value="input.jhtml",method=RequestMethod.GET)
	public String input(HttpServletRequest request,Model model){
		int id = RequestUtil.getIntParameter(request, "id", 0);
		if(id > 0){
			Goods goods = goodsService.findGoodsById(id);
			if(goods == null){
				log.error("商品信息不存在id="+id);
				return ERROR;
			}
			List<Accessory> accessoryList = goodsService.findAccessoryListByGoodsId(id);
			model.addAttribute("goods", goods);
			model.addAttribute("accessoryList", accessoryList);
		}
		List<Category> categoryList = categoryService.findAllCategoryList();
		model.addAttribute("categoryList", categoryList);
		model.addAttribute("saveImagUrl",AccessoryAction.SAVE_IMAG_URL);
		return "goods_input";
	}
	
	@RequestMapping(value="save.jhtml",method=RequestMethod.POST)
	public String save(HttpServletRequest request,HttpServletResponse response){
		int id = RequestUtil.getIntParameter(request, "id", 0);
		int cid = RequestUtil.getIntParameter(request, "cid", 0);
		String goodsname = RequestUtil.getStringParameter(request, "goodsname", null);
		float goodsfreight = RequestUtil.getFloatParameter(request, "goodsfreight", 0f);
		String goodsbrief = RequestUtil.getStringParameter(request, "goodsbrief", null);
		String goodsabout = RequestUtil.getStringParameter(request, "goodsabout", null);
		Goods goods= new Goods(id,cid,goodsname,goodsfreight,goodsbrief,goodsabout);
		try{
			id = goodsService.saveOrUpdate(goods);
			String imgstr = RequestUtil.getStringParameter(request, "imgs", null);
			if(imgstr != null){
				int[] imgs = StringUtil.string2IntArray(imgstr, ";");
				try{
					goodsService.saveGoodsAccessory(id,imgs);
				}catch(Exception e){
					log.error("商品与附件关联失败:"+goods+";imgs="+StringUtil.intArray2String(imgs,";"),e);
					return this.ajaxJsonErrorMessage(response, "系统繁忙，请稍后再试");
				}
			}
			return this.ajaxJsonSuccessMessage(response, "操作成功");
		}catch(Exception e){
			log.error("操作失败:"+goods,e);
			return this.ajaxJsonErrorMessage(response, "系统繁忙，请稍后再试");
		}
	}
	
	@RequestMapping(value="del.jhtml",method=RequestMethod.POST)
	public String del(HttpServletRequest request,HttpServletResponse response){
		int id = RequestUtil.getIntParameter(request, "id", 0);
		if(id <= 0){
			return this.ajaxJsonErrorMessage(response, "您选择的商品不存在");
		}
		try{
			goodsService.deleteById(id);
			log.info("删除商品成功id="+id);
			return this.ajaxJsonSuccessMessage(response, "删除商品成功");
		}catch(Exception e){
			log.error("删除商品失败 id="+id,e);
			return this.ajaxJsonErrorMessage(response, "系统繁忙，请稍后再试");
		}
	}
	
	
}
